/**
 * 
 */
package fr.limsi.seven;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.TransferHandler;

/**
 * @author durand
 */
public class StarPanelCompare extends JPanel implements MouseMotionListener {

	/**
	 * Auto-generated serial version UID number
	 */
	private static final long serialVersionUID = 5556015960607544457L;

	StarCompare center;

	JPanel list;
	Vector<JPanel> contenuList;

	Container upper;

	StarPanelCompare(Collection<PyramideVariable> struct, Container upper) {
		super();

		center = new StarCompare(struct, this);
		this.upper = upper;

		contenuList = new Vector<JPanel>();
		list = new JPanel();

		this.setLayout(new BorderLayout());
		this.add(center, BorderLayout.CENTER);
		this.add(list, BorderLayout.SOUTH);

		final Container sup = upper;

		// on cree les deux fleches
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(1, 2));
		JButton btGauche = new JButton("<");
		btGauche.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				center.rotateStarDirect();
				sup.repaint();
			}
		});
		JButton btDroit = new JButton(">");
		btDroit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				center.rotateStarUndirect();
				sup.repaint();
			}
		});
		this.addKeyListener(center);
		btGauche.addKeyListener(center);
		btDroit.addKeyListener(center);
		jp.add(btGauche);
		jp.add(btDroit);
		contenuList.add(jp);

		// on l'affiche
		list.setLayout(new GridLayout(contenuList.size(), 1));
		for (int i = 0; i < contenuList.size(); i++) {
			list.add(contenuList.get(i));
		}
		this.validate();
		upper.repaint();

		setTransferHandler(new StarCompareTransferHandler());// pour le drag and drop
		this.addMouseMotionListener(this);
	}

	public void addStar(StarDrawer st) {
		// on regarde si il n'est pas deja dedans
		if (center.asAlreadyStar(st.getMyLines().iterator().next().maVar))
			return;
		// on l'ajoute
		center.addStar(st);
		// on refait le pane de description des couleurs/noms/masquage
		JPanel t = createControlLine(st);
		contenuList.add(1, t);
		list.removeAll();
		list.setLayout(new GridLayout(contenuList.size(), 1));
		for (int i = 0; i < contenuList.size(); i++) {
			list.add(contenuList.get(i));
		}
		this.validate();
		upper.repaint();
	}

	JPanel createControlLine(StarDrawer st) {
		JPanel jp = new JPanel();
		JButton btCoul = new JButton(" ");
		JButton btSuppr = new JButton("masquer");
		JLabel lblNode = new JLabel(st.getNode().getName());

		final StarDrawer stD = st;
		final JPanel jpan = jp;
		final StarPanelCompare me = this;
		btCoul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color temp = JColorChooser.showDialog(getFocusCycleRootAncestor(), "Choissisez la couleur de cette variable.", stD.getCouleur());
				if (temp != null) {
					stD.setCouleur(temp);
					((Component) e.getSource()).setBackground(temp);
					upper.repaint();
				}
			}
		});
		btCoul.setBackground(stD.getCouleur());
		btCoul.addKeyListener(center);

		btSuppr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				center.removeStar(stD);
				contenuList.remove(jpan);
				list.removeAll();
				list.setLayout(new GridLayout(contenuList.size(), 1));
				for (int i = 0; i < contenuList.size(); i++) {
					list.add(contenuList.get(i));
				}
				me.validate();
				upper.repaint();
			}
		});
		btSuppr.addKeyListener(center);

		jp.setLayout(new GridLayout(1, 3));
		jp.add(btCoul);
		jp.add(lblNode);
		jp.add(btSuppr);
		return jp;
	}

	public BufferedImage createImageForCopy() {
		// on cree l'image
		BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D g = (Graphics2D) image.getGraphics();
		// on cree l'etoile
		g.fillRect(0, 0, getWidth(), getHeight());
		center.paintComponent(g);
		// on met la legende
		int height = center.getHeight();
		int width = 0;
		Component current;
		for (int i = 1; i < contenuList.size(); i++) {
			// text
			current = contenuList.get(i).getComponent(1);
			g.setColor(Color.BLACK);
			g.drawString(((JLabel) current).getText() + " : ", 1, height + 13); // +13 pour abaisser les lettres au niveau de la couleur
			width = g.getFontMetrics().stringWidth(((JLabel) current).getText() + " : ") + 5;
			// couleur
			current = contenuList.get(i).getComponent(0);
			g.setColor(current.getBackground());
			g.fillRect(width, height, 50, 20);

			height += 20;
		}

		return image;
	}

	public void mouseDragged(MouseEvent arg0) {
		this.getTransferHandler().exportAsDrag(this, arg0, TransferHandler.COPY);
	}

	public void mouseMoved(MouseEvent arg0) {
	}

	public StarCompare getStarFrame() {
		return center;
	}

}
